import { plugin, verc, config, data } from '../api/api.js'
import fs from 'node:fs'
import {
  existplayer,
  Write_player,
  Read_player
} from '../model/starrysky.js'
import {
  get_player_img
} from '../model/starrysky.js';
import YAML from 'yaml'
import { __PATH } from '../model/starrysky.js';

export class StarrySkyStart extends plugin 
{
	constructor () 
	{
    		super
		({
      			name: 'StarrySkyStart',
      			dsc: 'StarrySkyStart',
      			event: 'message',
      			priority: 9,
      			rule:
			[{
        				reg: '^#走向星空$',
        				fnc: 'StarrySkyStart'
      			},{
        				reg: '^#修改昵称.*$',
        				fnc: 'StarrySkyId'
      			},{
        				reg: '^#修改口号.*$',
        				fnc: 'StarrySkySlogan'
      			},{
        				reg: '^#修改性别.*$',
        				fnc: 'StarrySkysSex'
      			}]
    		})
	}

	async StarrySkyStart ( e ) 
	{
		let user_qq = e.user_id.toString().replace('qg_','');
    		let ifexistplay = await existplayer(user_qq);
    		if (ifexistplay)
		{
			e.reply("猪猪！你都注册过啦！！！");
    			let img = await get_player_img(e);
    			e.reply(img);
			return false;
		}
		let s = await Read_player(1);
		s=s+1;
		await Write_player(1, s);
		const now = new Date();
		const year = now.getFullYear();
		const month = now.getMonth() + 1;
		const date = now.getDate();
		const hour = now.getHours();
		const minute = now.getMinutes();
		const second = now.getSeconds();
		let data={
			ID: '路人'+s.toString(),
			Declaration: null,
			RegistrationDate: year.toString()
				+'.'+(month<10 ? '0'+month.toString(): month.toString())
				+'.'+(date<10 ? '0'+date.toString(): date.toString())
				+'.'+(hour<10 ? '0'+hour.toString(): hour.toString())
				+':'+(minute<10 ? '0'+minute.toString(): minute.toString())
				+':'+(second<10 ? '0'+second.toString(): second.toString()),
			QQ: user_qq,
			UID: s,
			Sex: 0,

			//基因境界
			a_level: 0,
			//灵魂境界
			b_level: 0,
			//原能境界
			c_level: 0,
			//星空境界
			d_level: 0, 
			//原能星辰
			cd_star: 0, 
			//法则境界
			e_level: 0,
			//法则感悟
			e_think: [
				{庚金: 0},{甲木: 0},{壬水: 0},{丙火: 0},{戊土: 0},
				{辛金: 0},{乙木: 0},{癸水: 0},{丁火: 0},{己土: 0},
				{阳: 0},{阴: 0},{光: 0},{暗: 0},
				{空间: 0},{时间: 0},
				{混沌: 0}
			],
			//修炼速度
			CultivationV: 0, 
			 

			//天赋
			talent:[],
			//功法
			gf: [],
			//秘技
			mj:[],

			//伴生秘宝
			bsmb:[],
			//秘宝
			mb:[],
			//装备
			equipment:[
				{武器: null},{头部: null},{手部: null},{躯干: null},{脚部: null}
			],
			//星石
			starstone: 0,

			//生命值上限 
			mhp: 100,
			//当前生命值
			nhp: 100,
			//攻击力
			atk: 100,
			//防御力
			def: 100, 
			//暴击率
			ctrt: 0.1,
			//暴击伤害
			ctdm: 1
		}
    		await Write_player(user_qq, data);
		e.reply('注册成功！欢迎来到星空！！！');
    		let img = await get_player_img(e);
    		e.reply(img);
		return false;	
	}
	
  	async Change_player_name(e) 
	{
    		if (!verc({ e })) 
		{
			return false;
		}
    		let user_qq = e.user_id.toString().replace('qg_','');
    		//有无存档
    		let ifexistplay = await existplayer(usr_qq);
    		if (!ifexistplay) 
		{
			return false;
		}
    		//检索方法
    		var reg = new RegExp(/改名|设置道宣/);
    		let func = reg.exec(e.msg);
    		//
    		if (func == '改名') 
		{
      		let new_name = e.msg.replace('#改名', '');
      new_name = new_name.replace(' ', '');
      new_name = new_name.replace('+', '');
      if (new_name.length == 0) {
        e.reply('改名格式为:【#改名张三】请输入正确名字');
        return false;
      } else if (new_name.length > 8) {
        e.reply('玩家名字最多八字');
        return false;
      }
      let player = {};
      let now = new Date();
      let nowTime = now.getTime(); //获取当前日期的时间戳
      //let Yesterday = await shijianc(nowTime - 24 * 60 * 60 * 1000);//获得昨天日期
      let Today = await shijianc(nowTime);
      let lastsetname_time = await redis.get(
        'xiuxian:player:' + usr_qq + ':last_setname_time'
      ); //获得上次改名日期,
      lastsetname_time = parseInt(lastsetname_time);
      lastsetname_time = await shijianc(lastsetname_time);
      if (
        Today.Y == lastsetname_time.Y &&
        Today.M == lastsetname_time.M &&
        Today.D == lastsetname_time.D
      ) {
        e.reply('每日只能改名一次');
        return false;
      }
      player = await Read_player(usr_qq);
      if (player.灵石 < 1000) {
        e.reply('改名需要1000灵石');
        return false;
      }      
      
      let arr=await alluser()
      for(let z=0;z<arr.length;z++){
        let B=await Read_player(arr[z])
        if(new_name==B.名号){
          e.reply("这世间已存在该名字的人:"+arr[z])
          return
        }
      }
      
      player.名号 = new_name;
      redis.set('xiuxian:player:' + usr_qq + ':last_setname_time', nowTime); //redis设置本次改名时间戳
      player.灵石 -= 1000;
      await Write_player(usr_qq, player);
      //Add_灵石(usr_qq, -100);
      this.Show_player(e);
      return false;
    }
    //设置道宣
    else if (func == '设置道宣') {
      let new_msg = e.msg.replace('#设置道宣', '');
      new_msg = new_msg.replace(' ', '');
      new_msg = new_msg.replace('+', '');
      if (new_msg.length == 0) {
        return false;
      } else if (new_msg.length > 50) {
        e.reply('道宣最多50字符');
        return false;
      }
      let player = {};
      let now = new Date();
      let nowTime = now.getTime(); //获取当前日期的时间戳
      //let Yesterday = await shijianc(nowTime - 24 * 60 * 60 * 1000);//获得昨天日期
      //
      let Today = await shijianc(nowTime);
      let lastsetxuanyan_time = await redis.get(
        'xiuxian:player:' + usr_qq + ':last_setxuanyan_time'
      );
      //获得上次改道宣日期,
      lastsetxuanyan_time = parseInt(lastsetxuanyan_time);
      lastsetxuanyan_time = await shijianc(lastsetxuanyan_time);
      if (
        Today.Y == lastsetxuanyan_time.Y &&
        Today.M == lastsetxuanyan_time.M &&
        Today.D == lastsetxuanyan_time.D
      ) {
        e.reply('每日仅可更改一次');
        return false;
      }
      //这里有问题，写不进去
      player = await Read_player(usr_qq);
      player.宣言 = new_msg; //
      redis.set('xiuxian:player:' + usr_qq + ':last_setxuanyan_time', nowTime); //redis设置本次设道置宣时间戳
      await Write_player(usr_qq, player);
      this.Show_player(e);
      return false;
    }
  }
}
